package com.niuke;

import com.leetCode.tree.TreeNode;


import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-12-14 18:27:48
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-12-14     张李鑫                     1.0         1.0 Version
 */
public class JZ36 {
    public static TreeNode Convert(TreeNode pRootOfTree) {
        ArrayList<TreeNode> list = new ArrayList<>();
        process(pRootOfTree, list);
        list.get(0).left = null;
        for (int i = 1; i < list.size(); i++) {
            list.get(i).left = list.get(i - 1);
        }
        list.get(list.size() - 1).right = null;
        return list.get(0);
    }

    private static void process(TreeNode pRootOfTree, List<TreeNode> list) {
        if (pRootOfTree == null) {
            return;
        }
        process(pRootOfTree.left, list);
        list.add(pRootOfTree);
        process(pRootOfTree.right, list);

    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(10);
        TreeNode treeNode1 = new TreeNode(6);
        TreeNode treeNode2 = new TreeNode(14);
        TreeNode treeNode3 = new TreeNode(4);
        TreeNode treeNode4 = new TreeNode(8);
        TreeNode treeNode5 = new TreeNode(12);
        TreeNode treeNode6 = new TreeNode(16);

        treeNode.left = treeNode1;
        treeNode.right = treeNode2;

        treeNode1.left = treeNode3;
        treeNode1.right = treeNode4;

        treeNode2.left = treeNode5;
        treeNode2.right = treeNode6;

        System.out.println(Convert(treeNode));
    }

}
